
using System;
using System.Xml;

namespace Habazie.kf2.Net.protocol
{
	/// <summary>
	///  	Privacy IQ
	/// </summary>
	public class PrivacyIQ :  jabber.protocol.client.IQ
	{
		/// <summary>
		/// 	Create a privacy IQ stanza 	
		/// </summary>
		public PrivacyIQ (XmlDocument doc) : base(doc)
		{
			this.Query = new Privacy (doc);
		}
	}
	
	
	/// <summary>
	///  	Privacy query	
	/// </summary>
	public class Privacy : jabber.protocol.Element
	{
		public Privacy(XmlDocument doc)
			: base ("query", "jabber:iq:privacy", doc)
		{
		}
		
		
		public Privacy(string prefix, XmlQualifiedName qname, XmlDocument doc)
			: base (prefix, qname, doc)
		{
		}
	}
	
	
	/// <summary>
	///  	Privacy list	
	/// </summary>
	public class PrivacyList : jabber.protocol.Element
	{
		/// <summary>
		/// 	Create a new list element 	
		/// </summary>
		public PrivacyList(XmlDocument doc)
			: base ("list", "jabber:iq:privacy", doc)
		{
		}
		
		
		public PrivacyList(string prefix, XmlQualifiedName qname, XmlDocument doc)
			: base (prefix, qname, doc)
		{
		}
		
		
		/// <summary>
		/// 	List name 	
		/// </summary>
		public string ListName {
			get { return GetAttribute ("name"); }
			set { SetAttribute ("name", value); }
		}
		
	}		
	
	public enum ItemType {
		none,
		
		/// <summary>
		/// 	JabberID-based rule 	
		/// </summary>
		jid,
		
		/// <summary>
		/// 	Group-based rule 	
		/// </summary>
		group,
		
		/// <summary>
		/// 	Subscription based rule 	
		/// </summary>
		subscription
	}
	

	/// <summary>
	/// 	Rule action
	/// </summary>
	public enum ItemAction {
		allow, deny
	}
	
	
	/// <summary>
	/// 	Privacy rule item
	/// </summary>	
	public class PrivacyListItem : jabber.protocol.Element
	{
		public PrivacyListItem(XmlDocument doc)
			: base ("item", "jabber:iq:privacy", doc)
		{
		}
		
		
		public PrivacyListItem(string prefix, XmlQualifiedName qname, XmlDocument doc)
			: base (prefix, qname, doc)
		{
		}
		
		
		/// <summary>
		/// 	Rule type 	
		/// </summary>
		public ItemType Type {
			get {
				if (HasAttribute ("type"))
					return (ItemType) GetEnumAttr ("type", typeof (ItemType));
				else
					return ItemType.none;
			}
			set {
				if (value == ItemType.none)
					RemoveAttribute ("type");
				else
					SetAttribute ("type", value.ToString ());
			}
		}
		
		
		/// <summary>
		/// 	Rule value 	
		/// </summary>
		public string Value {
			get { return GetAttribute ("value"); }
			set { SetAttribute ("value", value); }
		}
		
		
		/// <summary>
		/// 	Rule action 	
		/// </summary>
		public ItemAction Action {
			get { return (ItemAction) GetEnumAttr ("action", typeof (ItemAction)); }
			set { SetAttribute ("action", value.ToString ()); }
		}
		

		/// <summary>
		/// 	Order in which this list is applied 	
		/// </summary>
		public uint Order {
			get { return System.UInt32.Parse (GetAttribute ("order")); }
			set { SetAttribute ("order", value.ToString ()); }
		}
		
		
		public bool Message {
			get { return (this["message"] != null); }
			set {
				if (value)
					this.AppendChild (this.OwnerDocument.CreateElement ("message"));
				else
					if (this["message"] != null)
						this.RemoveChild (this["message"]);
			}
		}
		
		
		public bool IQ {
			get { return (this["iq"] != null); }
			set {
				if (value)
					this.AppendChild (this.OwnerDocument.CreateElement ("iq"));
				else
					if (this["iq"] != null)
						this.RemoveChild (this["iq"]);
			}
		}
		
		
		public bool PresenceIn {
			get { return (this["presence-in"] != null); }
			set {
				if (value)
					this.AppendChild (this.OwnerDocument.CreateElement ("presence-in"));
				else
					if (this["presence-in"] != null)
						this.RemoveChild (this["presence-in"]);
			}
		}
		
		
		public bool PresenceOut {
			get { return (this["presence-out"] != null); }
			set {
				if (value)
					this.AppendChild (this.OwnerDocument.CreateElement ("presence-out"));
				else
					if (this["presence-out"] != null)
						this.RemoveChild (this["presence-out"]);
			}
		}
		
		
		public bool All {
			get { return ! (Message || IQ || PresenceIn || PresenceOut); }
			set { Message = !value; IQ = !value; PresenceIn = !value; PresenceOut = !value; }
		}
		
	}

}
